@extends('layouts.admin_common') @section('content')

    @if(app('env')=='production')
        <script src="{{ secure_asset('/js/sy_common.js') }}"></script>
    @else
        <script src="{{ asset('/js/sy_common.js') }}"></script>
    @endif

    <div class="breadcrumbs_container">{{
	Breadcrumbs::render('admin.tack_down') }}</div>
    <div class="system_tab_line"></div>

    <div class="lyx_search_table">

        <div class="row">
            <div class="col-sm-3">
                <button class="btn btn-primary button_normal" id="lyx_add_line">@lang('symessages.info.add_goods')</button>
            </div>
        </div>


        <div class="lyx_table">
            <div class="row lyx_table_title">
                <div class="col-sm-1"></div>
                <div class="col-sm-7">androJapan 棚卸処理</div>
                <div class="col-sm-4">出力日:{{toDate(time(),'Y-m-d')}}</div>
            </div>
            <div class="row">
                <div class="col-sm-1"></div>
                <div class="col-sm-6">
                    @lang('symessages.info.order_code'):{{$order_id}}
                </div>
            </div>

            <div class="lyx_alert">

            </div>

            <table class="table table-bordered">
                <thead>
                <tr>
                    <th scope="col">@lang('symessages.info.category')</th>
                    <th scope="col">@lang('symessages.info.product_code')</th>
                    <th scope="col">@lang('symessages.info.product_name')</th>
                    <th scope="col">@lang('symessages.info.price')</th>
                    <th scope="col">@lang('symessages.info.size')</th>
                    <th scope="col">@lang('symessages.info.physical_stock')</th>
                    <th scope="col">@lang('symessages.info.physical_stock')</th>
                    <th scope="col">差額</th>
                </tr>
                </thead>
                <tbody id="lyx_tbody">
                <tr>
                    <td>
                        <select class="form-control form-control-sm lyx_cat_select">
                            <option>select category</option>
                            @foreach($category as $cat)
                                <option value="{{$cat->category_id}}">{{$cat->category_name}}</option>
                            @endforeach
                        </select>
                    </td>
                    <td>
                        before select category
                    </td>
                    <td></td>
                    <td></td>
                    <td class="lyx_size_arr"></td>
                    <td class="lyx_size_num"></td>
                    <td width="100px">
                        <input class="form-control form-control-sm lyx_house_num" type="number" placeholder="">
                    </td>
                    <td></td>
                </tr>


                </tbody>
            </table>
            <div class="row">
                <div class="col-sm-8"></div>
                <div class="col-sm-4">
                    <button class="btn btn-primary" id="lyx_post_submit">提交</button>
                </div>
            </div>
        </div>

    </div>


    <script>

        $(function () {
            var postLineData = [];
            var total_sum_money = 0;//总金额
            var order_id = '{{$order_id}}';
            add_select_action();


            $('#lyx_post_submit').on('click',function () {
                postData();
            })

            //-选择分类
            function add_select_action() {
                $(".lyx_cat_select").off().change(function () {
                    var value = $(this).val();
                    var that = $(this).parent('td').next('td');
                    if (value == null || value == '' || value == undefined) {
                        that.html('select category');
                    } else {
                        that.html('loading...');
                        useCatgetGoods($(this).val(), that);
                    }
                });
            }


            //-选择产品
            function add_goods_select_action() {
                $(".lyx_goods_select").off().change(function () {
                    var value = $(this).val();
                    var that = $(this).parent('td').next('td');
                    if (value == null || value == '' || value == undefined) {
                        that.html('select goods');
                    } else {
                        that.html('loading...');
                        getGoodsInfo($(this).val(), that);
                    }
                });
            }

            //--添加数量
            function add_goods_num_action() {
                $(".lyx_house_num").off().change(function () {
                    var thatTr = $(this).parents('tr');
                    var num = $(this).val();
                    var that = $(this).parent('td').next('td');
                    var price = $(this).parent('td').prev('td').data('price');
//                    var total_house_num = thatTr.find('td').eq(3).html();
                    var total_house_num = thatTr.find('.lyx_size_num').html();
                    var surplus_num = parseInt(total_house_num) - parseInt(num);
                    that.html(surplus_num);
                    addDataToAry(thatTr, num);
                });
            }

            //--添加按钮
            $('#lyx_add_line').on('click', function () {
                addLine();
            })


            /**
             * 添加一条记录
             */
            function addLine() {
                var html = ['<tr>',
                    '                    <td>',
                    '                        <select class="form-control form-control-sm lyx_cat_select">',
                    '                            <option>select category</option>',
                    @foreach($category as $cat)
                        '                                <option value="{{$cat->category_id}}">{{$cat->category_name}}</option>',
                    @endforeach
                        '                        </select>',
                    '                    </td>',
                    '                    <td>',
                    '                        before select category',
                    '                    </td>',
                    '                    <td></td>',
                    '                    <td></td>',
                    '                    <td class="lyx_size_arr"></td>',
                    '                    <td class="lyx_size_num"></td>',
                    '                    <td width="100px">',
                    '                        <input class="form-control form-control-sm lyx_house_num" type="text" placeholder="">',
                    '                    </td>',
                    '                    <td></td>',
                    '                </tr>'].join("");
                $('#lyx_tbody').append(html);
                add_select_action();
            }

            /**
             *
             * @param cat_id 产品类别id
             * @param that 操作哪个dom
             */
            function useCatgetGoods(cat_id, that) {
                $.ajax({
                    url: '/admin/use_catget_goods',
                    type: 'post',
                    data: {cat_id: cat_id},
                    success: function (retData) {
                        var html1 = '<select class="form-control form-control-sm lyx_goods_select"><option value="">select goods</option>';
                        var html3 = '</select>';
                        var html2 = '';
                        if (retData.data.length > 0) {
                            $.each(retData.data, function (index, item) {
                                html2 = html2 + '<option value="' + item.goods_id + '">' + item.goods_code + '</option>';
                            })
                            var html_end = html1 + html2 + html3;
                        } else {
                            html_end = 'empty!'
                        }
                        $(that).html(html_end);
                        add_goods_select_action();
                    }
                })
//            lyxAlert('alert-primary', '你好', 3000);
            }


            function getGoodsInfo(goods_id, that) {
                $.ajax({
                    url: '/admin/get_goods_info',
                    type: 'post',
                    data: {goods_id: goods_id},
                    success: function (retData) {
                        var two = that.next();
                        var three = that.next();
                        that.html(retData.data.goods_name);
//                        two.html(retData.data.total_num);
                        three.html('¥' + retData.data.price);
                        three.data('price', retData.data.price);
                        that.parents('tr').data('goods_id',goods_id);
                        add_goods_num_action();
                        getSizeArr(goods_id, that);
                    }
                })
//            lyxAlert('alert-primary', '你好', 3000);
            }

            /**
             * 尺寸数据
             */
            function getSizeArr(id, that) {
                $.ajax({
                    url: '/admin/get_model_info',
                    type: 'post',
                    data: {goods_id: id},
                    success: function (retData) {
                        var theme = that.parents('tr');
                        var html = size_html(retData.data);
                        theme.find('.lyx_size_arr').html(html);
                        theme.data('model_info',retData.data[0]);
                        add_size_action();
                        getSizeGoodsNum(id,retData.data[0].goods_model_id,that)
                    }
                })
            }

            /**
             * 尺寸的代码片段
             */
            function size_html(data) {
                var html_1 = '<select class="form-control form-control-sm lyx_size_select">';
                var html_2 = ''
                var html_3 = '</select>';
                $.each(data, function (index, item) {
                    html_2 += "<option value='" + JSON.stringify(item) + "'>" + item.color_name+"/"+item.size_name + "</option>";
                })
                return html_1 + html_2 + html_3;
            }

            function add_size_action() {
                $(".lyx_size_select").off().change(function () {
                    var value = $(this).val();
                    value = eval('(' + value + ')');
                    var goods_id = $(this).parents('tr').data('goods_id');
                    $(this).parents('tr').data('model_info',value);

                    getSizeGoodsNum(goods_id,value.goods_model_id,$(this))
                    sumToalMoney();
                });
            }

            function getSizeGoodsNum(goods_id,model_id,that) {
                hdAjax({
                    url:'user_size_get_goods_num',
                    data:{goods_id:goods_id,model_id:model_id},
                    success:function (retData) {
                        if(retData.data != null){
                            that.parents('tr').find('.lyx_size_num').html(retData.data.num);
                        }else{
                            that.parents('tr').find('.lyx_size_num').html(0);
                        }
                    }
                })
            }

            /**
             * 添加数据到数组里面
             */
            function addDataToAry(that, num) {
                var category_id = that.find('.lyx_cat_select').val();
                var goods_id = that.find('.lyx_goods_select').val();
                var goods_name = that.find('td').eq(2).html();
                var total_house_num = that.find('td').eq(3).html();
                var price = that.find('td').eq(4).data('price');
                console.log(category_id, goods_id, goods_name, price);
                var json = {
                    category_id: category_id,
                    goods_id: goods_id,
                    goods_name: goods_name,
                    total_num :total_house_num,
                    price: price,
                    goods_num:num,
                    total_money: num * price
                };
                that.data('onedata', json);
                sumToalMoney();
            }

            /**
             * 计算总价，并且把数据放到数据中
             */
            function sumToalMoney() {
                postLineData = [];
                $('tr').each(function () {
                    var j = $(this).data('onedata');
                    if (j != undefined && j != null && j != '') {
                        total_sum_money = total_sum_money + j.total_money;
                        var modelInfo = $(this).data('model_info');
                        j.model_info = modelInfo;
                        postLineData.push(j);
                    }
                })
                $('#total_sum_money').html('¥' + total_sum_money);
            }


            function postData() {
                $address = $('#lyx_p_address').val();
                $alias = $('#lyx_p_address_alias').val();
                $fax = $('#lyx_p_fax').val();
                $goods_date = $('#lyx_p_goods_date').val();
                $mobile = $('#lyx_p_mobile').val();
                $postal = $('#lyx_p_postal_code').val();
                $weituo_date = $('#lyx_p_weituo_date').val();

                $company_id = $('#lyx_companys').val();
                $company_name = $('#lyx_companys').find("option:selected").text();

                $shop_id = $('.lyx_shop_select').val();
                $shop_name = $('.lyx_shop_select').find("option:selected").text();

                console.log($address,$alias,$fax,$goods_date,$mobile,$postal,$weituo_date,postLineData);


                hdAjax({
                    url:'take_stock',
                    type:'post',
                    data:{
                        order_id:order_id,
                        goods_list:postLineData,
                        type:1
                    },
                    success:function (retData) {
                        if(retData.code == 1001){
                            successAlert();
                        }else{
                            layuiTopMsg(retData.msg);
                        }
                    }
                })

            }


        })

    </script>
@endsection

